<?php

/**
 * IndexController is the default controller for this application
 */
class SettingsController extends ControllerBase {
	/*
	 * SettingsDataService Object
	 */
	protected $settingsDataService;
	
	/**
	 * UsersDataService Object
	 *
	 * @var UsersDataService object
	 */
	public $usersDataService = null;
	
	/**
	 *
	 * PackageOptionDataService Object
	 * @var PackageOptionsDataService
	 */
	private $packageOptionDataService = null;
	
	/**
	 * CountryDataService Object
	 *
	 * @var CountryDataService object
	 */
	public $countryDataService = null;
	
	public function __construct() {
		parent::__construct ();
		
		$authentication = new Authentication ();
		//Only administrator can be right
		if ($authentication->isSuperAdministrator () == false) {
			
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$this->settingsDataService = new SettingsDataService ();
		
		// Get status list of users
		$this->smarty->assign ( 'statusList', UsersEntity::getStatusList () );
		
		// Get status list of users
		$this->smarty->assign ( 'userTypeList', UsersEntity::getMemberUserTypeList () );
		
		// New Users Data Service
		$this->usersDataService = new UsersDataService ();
		
		// Init Country DataService
		$this->countryDataService = new CountryDataService ();
		
		$this->packageOptionDataService = new PackageOptionsDataService ();
		
		// Set page name
		$this->setPageName ( 'settings' );
		$this->smarty->assign ( 'type', 1 );
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 */
	public function indexAction() {
		//Redirect to generalAction
		$this->generalAction ();
	}
	public function settingAction(){
		// Error flag
		$isError = false;
		$error = "";
		$aError = array();
		$bError = false;
		// Get data
		$feeSettingsEntity = $this->settingsDataService->getItem ( FEE_SERVICE );
		$googleApiSettingsEntity = $this->settingsDataService->getItem ( GOOGLE_API_CODE );	
		$curencySettingsEntity = $this->settingsDataService->getItem ( CURRENCY_USD_VND );		
		$action  = $this->getPost('save', "");
		// Get Email field
		if (!empty ($action)){
			$fee = $this->getPost ( 'fee', "" );
			if(!is_numeric($fee) || !(intval($fee) > 0)){
				$bError =  true;
				$aError = array_merge($aError, array('Phí dịch vụ phải là số và lớn hơn 0'));
			}
			$googleapi = $this->getPost ( 'googleapi', "" );
			if($googleapi == ''){
				$bError =  true;
				$aError = array_merge($aError, array('Google api không được bỏ trống'));
			}
			$currency = $this->getPost ( 'currency', "" );
			if(!is_numeric($currency) || !(intval($currency) > 0)){
				$bError =  true;
				$aError = array_merge($aError, array('tỷ giá phải là số và lớn hơn 0'));
			}
		}
		if (!empty ($action) && !$bError) {

						
			$feeSettingsEntity->value = $fee;
			$this->settingsDataService->update ( $feeSettingsEntity );
			$googleApiSettingsEntity->value = $googleapi;
			$this->settingsDataService->update ( $googleApiSettingsEntity );
			$curencySettingsEntity->value = $currency;
			$this->settingsDataService->update ( $curencySettingsEntity );
			
			$isError = false;
			// Show a message
			$error = 'The settings has been updated successfully';
		
		}
		$this->smarty->assign ( 'error', $error );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign ( 'aError', $aError);
		$this->smarty->assign ( 'bError', $bError );
		$this->smarty->assign ( 'fee', $feeSettingsEntity->value );
		$this->smarty->assign ( 'googleapi', $googleApiSettingsEntity->value );
		$this->smarty->assign ( 'currency', $curencySettingsEntity->value );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Settings/setting.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	public function generalAction() {
		// Error flag
		$isError = false;
		$error = "";
		
		// Get data
		$emailSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_ADMIN_EMAIL );
		$nameSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_ADMIN_NAME );
		
		// check data
		if (empty ( $emailSettingsEntity ) || empty ( $nameSettingsEntity )) {
			$isError = true;
			$error = "There's no EMAIL and NAME settings for Administrator.";
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// submit
		if (! empty ( $actionForm ) && $this->validateActionKey ()) {
			if ($actionForm == "email_update") {
				// Get Email field
				$email = $this->getPost ( 'email', "" );
				$email = trim ( $email );
				
				// Get name field
				$name = $this->getPost ( 'name', "" );
				$name = trim ( $name );
			}
			
			$emailSettingsEntity->value = $email;
			$this->settingsDataService->update ( $emailSettingsEntity );
			$nameSettingsEntity->value = $name;
			$this->settingsDataService->update ( $nameSettingsEntity );
			
			$isError = false;
			// Show a message
			$error = 'The settings has been updated successfully';
		
		}
		
		// create session
		$this->createActionKey ();
		
		$arrElement = array ();
		$arrElement ["userType"] = ROLE_ADMIN;
		
		// Get all items of users
		$itemList = $this->usersDataService->getItems ( $arrElement );
		
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'this', $this );
		
		$this->smarty->assign ( 'error', $error );
		$this->smarty->assign ( 'isError', $isError );
		
		$this->smarty->assign ( 'email', $emailSettingsEntity->value );
		$this->smarty->assign ( 'name', $nameSettingsEntity->value );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Settings/general.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Update settings Payment
	 * Enter description here ...
	 */
	public function paymentAction() {
		// Error flag
		$isError = false;
		$error = "";
		
		// Get Paypal Settings
		$paypalUsernameSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_USERNAME );
		$paypalPasswordSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_PASSWORD );
		$paypalSignatureSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_SIGNATURE );
		$paypalTestModeSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_TESTMODE );
		$paypalEnableSesttingEntity = $this->settingsDataService->getItem ( SETTINGS_PAYPAL_ENABLE );
		
		// Get 2Checkout Settings
		$checkoutUsernameSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_USERNAME );
		$checkoutPasswordSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_PASSWORD );
		$checkoutTestModeSettingsEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_TESTMODE );
		$chekoutEnableSettingEntity = $this->settingsDataService->getItem ( SETTINGS_2CHECKOUT_ENABLE );
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// submit
		if (! empty ( $actionForm ) && $this->validateActionKey ()) {
			if ($actionForm == "paypal_update") {
				// Get API Username
				$paypal_username = $this->getPost ( 'username', "" );
				$paypal_username = trim ( $paypal_username );
				
				// Get API Password
				$paypal_password = $this->getPost ( 'password', "" );
				$paypal_password = trim ( $paypal_password );
				
				// Get API Signature
				$paypal_signature = $this->getPost ( 'signature', "" );
				$paypal_signature = trim ( $paypal_signature );
				
				$paypal_testmode = $this->getPost ( 'testmode', "0" );
				
				if ($paypal_testmode == "on") {
					$paypal_testmode = "1";
				}
				
				$testMode = ($paypal_testmode == "1") ? true : false;
				
				$paypal_enable = $this->getPost ( 'paypal_enable', "0" );
				if ($paypal_enable == "on") {
					$paypal_enable = "1";
				}
				$enable = ($paypal_enable == "1") ? true : false;
				
				$message = "";
				//Validate Paypal settings
				$isValid = $this->settingsDataService->validatePaypalSettings ( $paypal_username, $paypal_password, $paypal_signature, $message, $testMode );
				
				if (! $isValid) {
					$isError = true;
					$error = $message;
					
					//Disable this Payment Gateway
					$paypalEnableSesttingEntity->value = "0";
					$this->settingsDataService->update ( $paypalEnableSesttingEntity );
				} else {
					$paypalUsernameSettingsEntity->value = $paypal_username;
					$this->settingsDataService->update ( $paypalUsernameSettingsEntity );
					$paypalPasswordSettingsEntity->value = $paypal_password;
					$this->settingsDataService->update ( $paypalPasswordSettingsEntity );
					$paypalSignatureSettingsEntity->value = $paypal_signature;
					$this->settingsDataService->update ( $paypalSignatureSettingsEntity );
					$paypalTestModeSettingsEntity->value = $paypal_testmode;
					$this->settingsDataService->update ( $paypalTestModeSettingsEntity );
					$paypalEnableSesttingEntity->value = $paypal_enable;
					$this->settingsDataService->update ( $paypalEnableSesttingEntity );
					
					$isError = false;
					// Show a message
					$error = 'The settings has been updated successfully';
				}
			} else if ($actionForm == "2checkout_update") {
				// Get API Username
				$checkout_username = $this->getPost ( 'username', "" );
				$checkout_username = trim ( $checkout_username );
				
				// Get API Password
				$checkout_password = $this->getPost ( 'password', "" );
				$checkout_password = trim ( $checkout_password );
				$checkout_testmode = $this->getPost ( 'testmode', "0" );
				if ($checkout_testmode == "on") {
					$checkout_testmode = "1";
				}
				$testMode = ($checkout_testmode == "1") ? true : false;
				
				$checkout_enable = $this->getPost ( 'checkout_enable', "0" );
				if ($checkout_enable == 'on') {
					$checkout_enable = "1";
				}
				$enable = ($checkout_enable == "1") ? true : false;
				
				$message = "";
				//Validate Paypal settings
				$isValid = $this->settingsDataService->validate2CheckoutSettings ( $checkout_username, $checkout_password, $message, $testMode, $enable );
				
				if (! $isValid) {
					$isError = true;
					$error = $message;
					
					// Disable 2Checkout Gateway
					$chekoutEnableSettingEntity->value = "0";
					$this->settingsDataService->update ( $chekoutEnableSettingEntity );
				
				} else {
					$checkoutUsernameSettingsEntity->value = $checkout_username;
					$this->settingsDataService->update ( $checkoutUsernameSettingsEntity );
					$checkoutPasswordSettingsEntity->value = $checkout_password;
					$this->settingsDataService->update ( $checkoutPasswordSettingsEntity );
					$checkoutTestModeSettingsEntity->value = $checkout_testmode;
					$this->settingsDataService->update ( $checkoutTestModeSettingsEntity );
					$chekoutEnableSettingEntity->value = $checkout_enable;
					$this->settingsDataService->update ( $chekoutEnableSettingEntity );
					
					$isError = false;
					// Show a message
					$error = 'The settings has been updated successfully';
				}
			}
		
		}
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'error', $error );
		$this->smarty->assign ( 'isError', $isError );
		
		$this->smarty->assign ( 'paypal_username', ($isError == true && isset ( $paypal_username )) ? $paypal_username : $paypalUsernameSettingsEntity->value );
		$this->smarty->assign ( 'paypal_password', ($isError == true && isset ( $paypal_password )) ? $paypal_password : $paypalPasswordSettingsEntity->value );
		$this->smarty->assign ( 'paypal_signature', ($isError == true && isset ( $paypal_signature )) ? $paypal_signature : $paypalSignatureSettingsEntity->value );
		$this->smarty->assign ( 'paypal_testmode', ($isError == true && isset ( $paypal_testmode )) ? $paypal_testmode : $paypalTestModeSettingsEntity->value );
		$this->smarty->assign ( 'paypal_enable', $paypalEnableSesttingEntity->value );
		
		$this->smarty->assign ( 'checkout_username', ($isError == true && isset ( $checkout_username )) ? $checkout_username : $checkoutUsernameSettingsEntity->value );
		$this->smarty->assign ( 'checkout_password', ($isError == true && isset ( $checkout_password )) ? $checkout_password : $checkoutPasswordSettingsEntity->value );
		$this->smarty->assign ( 'checkout_testmode', ($isError == true && isset ( $checkout_testmode )) ? $checkout_testmode : $checkoutTestModeSettingsEntity->value );
		$this->smarty->assign ( 'checkout_enable', $chekoutEnableSettingEntity->value );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Settings/payment.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	public function editPackageAction() {
		// get page
		$option = $this->getPost ( "id", "" );
		if (empty ( $option )) {
			$option = $this->getQuery ( "id", "" );
		}
		
		if (empty ( $option )) {
			
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Settings';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$packageOptionEntity = $this->packageOptionDataService->getById ( $option );
		
		if (empty ( $packageOptionEntity )) {
			
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Settings';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// submit
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			// Get name field
			$name = $this->getPost ( 'name', "" );
			$name = trim ( $name );
			
			// head title
			$price = $this->getPost ( 'price', "" );
			$cms = $this->getPost ( 'cms', "" );
			$cms = ($cms == "on") ? "1" : "0";
			$pageIncluded = $this->getPost ( 'pageIncluded', "" );
			
			$blogging = $this->getPost ( 'blogging', "" );
			$blogging = ($blogging == "on") ? "1" : "0";
			
			$gallery = $this->getPost ( 'gallery', "" );
			$gallery = ($gallery == "on") ? "1" : "0";
			
			$ecommerce = $this->getPost ( 'ecommerce', "" );
			$ecommerce = ($ecommerce == "on") ? "1" : "0";
			
			$news = $this->getPost ( 'news', "" );
			$news = ($news == "on") ? "1" : "0";
			
			$customDesign = $this->getPost ( 'customDesign', "" );
			$customDesign = ($customDesign == "on") ? "1" : "0";
			
			$ownDomain = $this->getPost ( 'ownDomain', "" );
			$ownDomain = ($ownDomain == "on") ? "1" : "0";
			
			$freeHosting = $this->getPost ( 'freeHosting', "" );
			$freeHosting = ($freeHosting == "on") ? "1" : "0";
			
			$freeBranding = $this->getPost ( 'freeBranding', "" );
			$freeBranding = ($freeBranding == "on") ? "1" : "0";
			
			$packageOptionEntity->name = $name;
			$packageOptionEntity->price = $price;
			$packageOptionEntity->cms = $cms;
			$packageOptionEntity->blogging = $blogging;
			$packageOptionEntity->pageIncluded = $pageIncluded;
			$packageOptionEntity->gallery = $gallery;
			$packageOptionEntity->ecommerce = $ecommerce;
			$packageOptionEntity->news = $news;
			$packageOptionEntity->customDesign = $customDesign;
			$packageOptionEntity->freeHosting = $freeHosting;
			$packageOptionEntity->brandingFree = $freeBranding;
			$packageOptionEntity->ownDomain = $ownDomain;
			$packageOptionEntity->modifiedDate = date ( "Y-m-d H:i:s" );
			
			// Check valid data input
			if (! empty ( $name ) && ! empty ( $price )) {
				
				try {
					$res = $this->packageOptionDataService->update ( $packageOptionEntity );
					
					$isError = false;
					
					// Show a message
					$error = 'The record has been updated successfully';
				} catch ( Exception $ex ) {
					
					// Ignore to process this case. Only show a message
					$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					$isError = true;
				}
			} else {
				$isError = true;
				$error = 'Please fill in all required fields';
			}
		}
		
		// create session
		$this->createActionKey ();
		
		$this->smarty->assign ( 'name', $packageOptionEntity->name );
		$this->smarty->assign ( 'price', $packageOptionEntity->price );
		$this->smarty->assign ( 'cms', $packageOptionEntity->cms );
		$this->smarty->assign ( 'pageIncluded', ($packageOptionEntity->pageIncluded == "-1") ? "Unlimited" : $packageOptionEntity->pageIncluded );
		$this->smarty->assign ( 'blogging', $packageOptionEntity->blogging );
		$this->smarty->assign ( 'ecommerce', $packageOptionEntity->ecommerce );
		$this->smarty->assign ( 'news', $packageOptionEntity->news );
		$this->smarty->assign ( 'customDesign', $packageOptionEntity->customDesign );
		$this->smarty->assign ( 'gallery', $packageOptionEntity->gallery );
		$this->smarty->assign ( 'ownDomain', $packageOptionEntity->ownDomain );
		$this->smarty->assign ( 'freeHosting', $packageOptionEntity->freeHosting );
		$this->smarty->assign ( 'freeBranding', $packageOptionEntity->brandingFree );
		
		$this->smarty->assign ( 'id', $option );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Settings/edit_package.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
}
